package com.xlf.common.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class Product extends Model<Product> {
	public static final Product dao = new Product();

	public Page<Product> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from product order by id asc");
	}

	public Page<Product> exeSearchSql(int pageNumber, int pageSize, String catalogId, String type, String brand, String status, String planType, Integer fId) {
		StringBuffer where = new StringBuffer(" from product where 1=1 ");
		List<Object> param = new ArrayList<Object>();

		if(catalogId != null) {
			where.append(" and c_id=?");
			param.add(Integer.parseInt(catalogId));
		}
		if(type != null && !"".equals(type.trim())) {
			where.append(" and type like ?");
			param.add("%" + type + "%");
		}
		if(brand != null && !"".equals(brand.trim())) {
			where.append(" and brand like ?");
			param.add("%" + brand + "%");
		}
		if(status != null && !"".equals(status.trim())) {
			if(Integer.parseInt(status) != -1) {
				where.append(" and status=?");
				param.add(Integer.parseInt(status));
			}
		}
		if(planType != null && !"".equals(planType.trim())) {
			where.append(" and planType<=?");
			param.add(Integer.parseInt(planType));
		}
		if(fId != null) {
			where.append(" and f_id=?");
			param.add(fId);
		}
		
		where.append(" order by p_id desc ");
		System.out.println(where.toString());
		return paginate(pageNumber, pageSize, "select *", where.toString(), param.toArray());
	}

}
